//上传头像 、图片
/* eslint-disable no-unused-vars */
let path = require("path");
let {
    user
} = require("../login/sign_schma.js");
let {
    images
} = require("./file_schema.js");
let {
    image
} = require("./files_schema.js");
module.exports = {
    fileFun: function (req, res, err) {
        console.log(req.body, 123);
        let users = req.body.user;
        let createTime = req.body.createTime;
        let type = req.body.type;
        let a = path.parse(req.files[0].destination).base
        let b = path.parse(req.files[0].path).base
        let name = b
        let src = `${req.body.src}/${a}/${b}`;
        console.log(req.body.src, a, b, src, "header");
        if (type == "header") {
            images.find({
                users: users
            }).then((data) => {
                if (data.length != 0) {
                    images.updateOne({
                        users: users
                    }, {
                        $set: {
                            src: src
                        }
                    }).then((datas) => {
                        console.log(datas);
                    })
                } else {
                    images.create({
                        name,
                        src,
                        users,
                        createTime,
                        type
                    }).then((datat) => {
                        console.log(datat);
                    })
                }

            })
            user.updateOne({
                user: users
            }, {
                $set: {
                    userHeader: src
                }
            }).then((data) => {
                console.log(data);
            })
            res.send("header");
        } else {
            image.find({
                src: src
            }).then((data) => {
                if (data.length != 0) {
                    image.updateOne({
                        users: users
                    }, {
                        $set: {
                            src: src
                        }
                    }).then((datas) => {
                        console.log(datas);
                    })
                } else {
                    image.create({
                        name,
                        src,
                        users,
                        createTime,
                        type
                    }).then((datat) => {
                        console.log(datat, 123);
                    })
                }
            })
            res.send(true);
        }
    }
}